package cn.maihe.elg.operation.centers.epoint.channel.service;

import cn.maihe.elg.operation.centers.epoint.channel.EpointBaseChannelService;
import cn.maihe.elg.operation.centers.epoint.dto.EpointBaseRespDTO;
import cn.maihe.elg.operation.centers.epoint.dto.channel.MeiShanClaimSrePayNoticeReq;
import cn.maihe.elg.operation.centers.epoint.enums.EpointChannelMethod;
import cn.maihe.elg.operation.centers.epoint.enums.EpointRespCodeEnum;
import cn.maihe.elg.operation.centers.supports.model.bo.GuaranteeClaimSettlementResultBo;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.model.enums.ClaimsStatus;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.RefundOrderInfoDO;
import cn.maihe.elg.operation.service.business.ProjectInfoService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.utils.DateUtil;
import cn.maihe.elg.operation.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Description 追偿结果通知
 * @Author SSX
 * @Date 2025/05/28
 */
@Service
@Slf4j
public class MeiShancClaimSrePayNoticeChannelService extends EpointBaseChannelService<MeiShanClaimSrePayNoticeReq, EpointBaseRespDTO> {
    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    private ProjectInfoService projectInfoService;
    @Override
    protected EpointChannelMethod methodEnum() {
        return EpointChannelMethod.claimsrepaynotice;
    }

    /**
     * 追偿结果通知
     */
    public InnerBaseResp<EpointBaseRespDTO> claimSrePayNotice(CenterNoEnum centerNoEnum, GuaranteeClaimSettlementResultBo bo) {
        String acceptOrderNo = bo.getAcceptOrderNo();
        RefundOrderInfoDO refundOrderInfo = bo.getRefundOrderInfo();
        AcceptOrderInfoDO acceptOrderInfoDO = acceptOrderInfoService.getByAcceptOrderNo(acceptOrderNo);
        if(Objects.isNull(acceptOrderInfoDO)){
            return InnerBaseResp.error(null,"查无此受理订单信息");
        }
        MeiShanClaimSrePayNoticeReq req = new MeiShanClaimSrePayNoticeReq();
        req.setApplyno(acceptOrderInfoDO.getCenterOrderId());
        req.setBaohanno(acceptOrderInfoDO.getGuaranteeNo());

        if(acceptOrderInfoDO.getClaimsStatus() == ClaimsStatus.SUCCESS.getStatus()){
            req.setRepaystatus("1");
            req.setIszbrcooperate("1");
        }else {
            req.setRepaystatus("0");
            req.setIszbrcooperate("0");
        }
        req.setRepayremark(acceptOrderInfoDO.getClaimsRemark());
        req.setRepaydate(DateUtil.formateDate(refundOrderInfo.getCompensateDate(), DateUtil.FORMAT_DATETIME));

        String refundAmount = refundOrderInfo.getPayerAmount();
        String amount = StringUtils.isBlank(refundAmount) ? StringUtil.toYuan(refundOrderInfo.getBidBond())
                : refundAmount;
        req.setRepayamount(amount);

        InnerBaseResp<EpointBaseRespDTO> innerBaseResp;
        try {
            EpointBaseRespDTO resp = this.doRequest(centerNoEnum, req, null);
            if (EpointRespCodeEnum.isSuccess(resp.getCode())) {
                innerBaseResp = InnerBaseResp.success(null);
            } else {
                innerBaseResp = InnerBaseResp.error(resp.getMessage());
            }
        } catch (Exception e) {
            log.error("追偿结果通知>> [{}] 异常:{}", centerNoEnum.getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("通知异常:" + e.getMessage());
        }
        return innerBaseResp;
    }
}
